<?php
class Track_model extends Base_Model
{
    private $baseurl='http://api.wayatrip.com';
	public function __construct()
    {
        $this->load->database();
    }


    /*public function getUserInfoById($uid)
    {
        $key_info = array("telActive","userIcon", "identityActive", "active", 'telCode', 'telNum', 'identityErea', 'identityName', 'identityPicture', 'email', 'failMsg', 'utype');
        $key_user_info = array('userAge', 'nationality', 'language', 'introduction', 'resideCity', 'resideTime', 'country', 'code', 'rcity', 'addr', 'userGender');


        $query = $this->db->query("select * from user_v2 as u1 LEFT JOIN user_v2_userinfo as u2 on u1.uid = u2.uid  WHERE u1.uid ={$uid}");
        $userInfo = $query->row_array();

        $baseInfo = array();
        foreach ($key_user_info as $v) {
            $baseInfo[$v] = empty($userInfo[$v]) ? '' : $userInfo[$v];
        }
        foreach ($key_info as $v) {
            $baseInfo[$v] = empty($userInfo[$v]) ? '' : $userInfo[$v];
        }

        if ($baseInfo['identityName'] == '留学生') {
            $data = explode('-|-', $baseInfo['identityErea']);
            $baseInfo['major'] = $data[0];
            $baseInfo['school'] = $data[1];
            $baseInfo['job'] = '';          
        }else{
        	$baseInfo['major'] = '';
        	$baseInfo['school'] = '';
        	$baseInfo['job'] = $baseInfo['identityErea'];
        }
        unset($baseInfo['identityErea']);
        $lang = array();
        foreach (explode(',', $userInfo['language']) as $v) {
            if ($v == 1) {
                $lang[] = '中文';
            } elseif ($v == 2) {
                $lang[] = '法文';
            } elseif ($v == 3) {
                $lang[] = '英文';
            }
        }
        $baseInfo['language'] = $lang;

        $userName = $userInfo['userFamily'].$userInfo['userName'];
        $baseInfo['realName'] = $userName;

        return $baseInfo;
    }*/
    /**
     * @param $id
     * @return mixed
     * 获取单个详情
     */
    public function detail($id)
    {

        $this->db->where('id', $id);
        $this->db->from('mod_track');
        $query = $this->db->get()->row_array();

        if(!empty($query['uid']))
        {
            $guider = $this->getUserInfoById($query['uid']);
        }else{
            return false;
        }

		$item['id'] = $query['id'];
		//玩伴id，聊天对象
		$item['rev_id']=$query['uid'];
		$item['type'] = $query['type'];
		$item['name'] = $query['name'];
        $item['city'] = $query['city'];
        $item['serviceMaxPersons'] = $query['serviceMaxPersons']."人";
        $item['duration']=$query['days'].'天';
        //背景图
        $item['bannerUrl']=$this->img($query['bannerUrl']);
        //照片墙
        $imgres = $this->getimages($id);
        $item['imageswall'] = $imgres['data'];
        $item['imagenum'] = $imgres['sum'];
        //基本信息
        $item['userIcon'] = $this->img($guider['userIcon']);
        $item['realName'] = $guider['realName'];
        //$item['identityActive'] = $guider['identityActive'];
        //$item['telActive'] = $guider['telActive'];
        //$item['resideCity'] = $guider['resideCity'];
        //$item['resideTime'] = $guider['resideTime'];
        $item['userAge'] = $guider['userAge'];

	    if($guider['userGender']=='m')
			$item['userGender'] = '男';
		elseif($guider['userGender']=='f')
			$item['userGender'] = '女';
		else
			$item['userGender'] = '';	

        $item['identityName'] = $guider['identityName'];
        //学校、专业、工作领域
        $item['major'] = $guider['major'];
        $item['school'] = $guider['school'];
        $item['job'] = $guider['job'];
        //玩伴介绍
        $item['introduction'] = nl2br($guider['introduction']);
        //集合地点
        $item['location'] = '与玩伴自行决定';
        //出行方式
        if($query['trafficSupportBus']==1)
        {	$item['chuxing']='公共交通';}
        elseif($query['trafficSupportCar']==1)
        {   $item['chuxing']='公共交通';}
        else {$item['chuxing']='自行选择';}       
        //路线简介
        $item['summary'] = nl2br($query['summary']);
        //行程描述
        $item['todolist'] = $this->gettodolist($id);
        //价格详情
        $item['serviceIcon'] = $this->img('/resource/images/service-icon.png');
        $item['priceService'] = round($query['priceService']/100,2).'€';
        $item['descService'] = '服务费用是除固定旅行成本(餐饮、交通、票务等)外给予玩伴的费用，其中包括陪游、讲解、行程安排和咨询等服务，价格不会随游客人数浮动';
        $item['ticketIcon'] = $this->img('/resource/images/ticket-icon.png');
        $item['priceTicket'] = round($query['priceTicket']/100,2).'€/人';
        $item['descTicket'] = nl2br($query['descTicket']);
        $item['trafficIcon'] = $this->img('/resource/images/traffic-icon.png');
        $item['priceTraffic'] = round($query['priceTraffic']/100,2).'€/人';
        $item['descTraffic'] = nl2br($query['descTraffic']);
        $item['foodIcon'] = $this->img('/resource/images/restauraut-icon.png');
        $item['priceFood'] = round($query['priceFood']/100,2).'€/人';
        $item['descFood'] = nl2br($query['descFood']);
        $item['otherIcon'] = $this->img('/resource/images/other-icon.png');
        $item['priceOther'] = round($query['priceOther']/100,2).'€/人';
        $item['descOther'] = nl2br($query['descOther']);
        

        return $item;
//        var_dump($item);die;

    }

    public function gettodolist($id)
    {
        $this->db->select('t.dayNo,t.scenicId,t.timeStart,t.timeStop,t.desc,s.name,s.subTitle,s.thumb,s.address,s.lat,s.lng');
        $this->db->where('trackId', $id);
        $this->db->from('mod_track_days as t');
        $this->db->join('mod_scenic_spot as s', 't.scenicId = s.id', 'left');
		$this->db->order_by('timeStart','asc');
		$query = $this->db->get()->result_array();
		$days=array();
	    $ret=array();
		foreach ($query as &$q)
        {
			$q['thumb'] = $this->img($q['thumb']);
            //构建dayContent
			$days[$q['dayNo']][$q['scenicId']]['scenicId']=$q['scenicId'];
			$days[$q['dayNo']][$q['scenicId']]['address']=$q['address'];
		    $days[$q['dayNo']][$q['scenicId']]['timeStart']=date("H:i",strtotime($q['timeStart']));
			$days[$q['dayNo']][$q['scenicId']]['timeStop']=date("H:i",strtotime($q['timeStop']));
			$days[$q['dayNo']][$q['scenicId']]['desc']=$q['desc'];
			$days[$q['dayNo']][$q['scenicId']]['name']=$q['name'];
			$days[$q['dayNo']][$q['scenicId']]['subTitle']=$q['subTitle'];
			$days[$q['dayNo']][$q['scenicId']]['thumb']=$q['thumb'];
			$days[$q['dayNo']][$q['scenicId']]['lat']=$q['lat'];
			$days[$q['dayNo']][$q['scenicId']]['lng']=$q['lng'];

			if(!isset($ret[$q['dayNo']]))
			{$ret[$q['dayNo']]['dayNo']=$q['dayNo'];}
			$ret[$q['dayNo']]['dayContent']=array_values($days[$q['dayNo']]);
        }
        return array_values($ret);
    }
    
    public function getimages($id)
    {
    	$sql="select s.id,s.images,s.name from mod_track_days as t
    		  left join mod_scenic_spot as s
    		  on s.id=t.scenicId where t.trackId=".$id;
    	$query=$this->db->query($sql)->result_array();
    	$ret=array();
    	$imgnum=0;
    	foreach ($query as $row){
    		$row['images']=trim($row['images']);
    		$row['images']=rtrim($row['images'],'|');
    		$imgs=explode('|', $row['images']);
    		$imgnum+=count($imgs);
    		foreach($imgs as $addr){
    			$ret[$addr]['image']=$this->img($addr);
    			$ret[$addr]['name']=$row['name'];
    		}
    	}
    	if(count($ret)>3) $ret=array_slice($ret, 0, 3);
    	return array('data'=>array_values($ret),'sum'=>$imgnum);
    }

    public function getbyid($id)
    {
        $this->db->where('id', $id);
        $this->db->from('mod_track');
        $query = $this->db->get()->row_array();
        return $query;
    }
    
    public function getimagewall($id)
    {
    	$sql="select s.id,s.images,s.name from mod_track_days as t
    		  left join mod_scenic_spot as s
    		  on s.id=t.scenicId where t.trackId=".$id;
    	$query=$this->db->query($sql)->result_array();
    	$ret=array();
    	//$imgnum=0;
    	foreach ($query as $row){
    		$row['images']=trim($row['images']);
    		$row['images']=rtrim($row['images'],'|');
    		$imgs=explode('|', $row['images']);
    		//$imgnum+=count($imgs);
    		foreach($imgs as $addr){
    			$ret[$addr]['image']=$this->img($addr);
    			$ret[$addr]['name']=$row['name'];
    		}
    	}

    	return array_values($ret);
    }

    
}
